package com.opennews.openplatform.myspringbootcore.util

import com.opennews.openplatform.myspringbootcore.dto.ApiResponse
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity

/**
 * Sends a successful response with the given payload.
 *
 * @param payload the response payload
 * @return ResponseEntity with OK status and payload
 */
fun <T> getSuccessResponse(payload: T): ResponseEntity<ApiResponse<T>> {
    return getDataResponse(true, HttpStatus.OK, payload) as ResponseEntity<ApiResponse<T>>
}

/**
 * Sends a successful response with the given http status.
 */
fun <T> getSuccessResponse(httpStatus: HttpStatus): ResponseEntity<ApiResponse<T?>> {
    return getDataResponse(true, httpStatus, null)
}

/**
 * Sends a successful response with default HttpStatus.NO_CONTENT.
 */
fun <T> getSuccessResponse(): ResponseEntity<ApiResponse<T?>> {
    return getDataResponse(true, HttpStatus.NO_CONTENT, null)
}

/**
 * Sends a successful response with the given http status and payload.
 */
fun <T> getSuccessResponse(httpStatus: HttpStatus, payload: T): ResponseEntity<ApiResponse<T>> {
    return getDataResponse(true, httpStatus, payload) as ResponseEntity<ApiResponse<T>>
}

fun <T> getErrorResponse(httpStatus: HttpStatus, payload: T): ResponseEntity<ApiResponse<T>> {
    return getDataResponse(false, httpStatus, payload) as ResponseEntity<ApiResponse<T>>
}

/**
 * Sends a response with the specified HTTP status and payload.
 *
 * @param httpStatus the HTTP status to send
 * @param payload    the response payload
 * @return ResponseEntity with the specified status and payload
 */
fun <T> getDataResponse(success: Boolean, httpStatus: HttpStatus, payload: T?): ResponseEntity<ApiResponse<T?>> {
    val response = ApiResponse(success, payload)
    return ResponseEntity.status(httpStatus).body(response)
}